﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;

namespace Yawf.Web.Utils {
	public class ResponseWrapper {
		private HttpResponse response;
		private IList<Object> resultObjects;
		private IList<String> resultNames;

		// Getters
		public HttpResponse Response { get { return response; } private set { response = value; } }
		public IList<Object> ResultObjects { get { return resultObjects; } private set { resultObjects = value; } }
		public IList<String> ResultNames { get { return resultNames; } private set { resultNames = value; } }
		public Stream OutputStream { get { return Response.OutputStream; } }
		public TextWriter Writer { get { return Response.Output; } }

		/// <summary>
		/// Only constructor of response wrapper
		/// </summary>
		/// <param name="r">Response object</param>
		public ResponseWrapper(HttpResponse r) {
			Response = r;
			ResultObjects = new List<Object>();
			ResultNames = new List<String>();
		}

		/// <summary>
		/// Adds an object to list of object
		/// </summary>
		/// <param name="obj">object to add</param>
		/// <param name="inName">name to use for saving object</param>
		public void Add(Object obj,String inName) {
			String name;

			ResultObjects.Add(obj);
			name = "";
			if (inName != null) {
				name = inName;
			}
			ResultNames.Add(name);
		}												// Add

		/// <summary>
		/// Gets index of an object looking by name
		/// </summary>
		/// <param name="name">Name to look for</param>
		/// <returns>Index of object, -1 if object not found</returns>
		private int GetIndexByName(String name) {
			IList<String> tmpNames;
			int i;

			tmpNames = ResultNames;
			for (i = 0; name != null && i < tmpNames.Count; i++) {
				if (name == tmpNames[i]) {
					return i;
				}
			}

			return -1;
		}										// GetIndexByName

		/// <summary>
		/// Gets object by name
		/// </summary>
		/// <param name="name">Name to look for</param>
		/// <returns>Object if found, null if not found</returns>
		public Object GetObjectByName(String name) {
			int i;

			i = GetIndexByName(name);
			if (i > -1 && i < ResultObjects.Count) {
				return ResultObjects[i];
			}

			return null;
		}							// GetObjectByName
	}
}
